<template>
  <div>

    <div style="margin: 10px 0">
      <el-input
          style="width: 200px"
          placeholder="请输入资产编号"
          suffix-icon="el-icon-search"
          v-model="assetNumber"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button
          type="primary"
          @click="exp"
          class="ml-5"
      >导出 <i class="el-icon-download"></i
      ></el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="maintainId" label="ID" width="80">
      </el-table-column>
      <el-table-column prop="maintainRecord" label="维修编号" width="180">
      </el-table-column>
      <el-table-column prop="assetNumber" label="资产编号" width="180">
      </el-table-column>
      <el-table-column prop="name" width="110" label="设备名称">
      </el-table-column>
      <el-table-column prop="type" label="设备类型">
      </el-table-column>
      <el-table-column prop="maintenance" label="维修员">
      </el-table-column>
      <el-table-column prop="maintenanceReason" label="维修原因">
      </el-table-column>
      <el-table-column width="225" prop="addtime" label="申请维修时间"></el-table-column>
      <el-table-column width="225" prop="updateTime" label="处理时间"></el-table-column>
      <el-table-column prop="result" label="结果">
      </el-table-column>
      <el-table-column label="结果登记">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" v-show="!scope.row.result">登记
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="50%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="维修编号">
          <el-input
              v-model="form.maintainRecord"
              style="width: 80px"
              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="资产编号">
          <el-input
              v-model="form.assetNumber"
              style="width: 200px"
              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
              v-model="form.name"

              :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备类型">
          <el-input v-model="form.type" :disabled="true"/>
        </el-form-item>
        <el-form-item label="维修员" required>
          <el-input v-model="form.maintenance"/>
        </el-form-item>
        <el-form-item label="维修结果" required>
          <el-select v-model="form.result"
                     style="width: 150px"
                     placeholder="请选择">
            <el-option label="正常" value="正常"></el-option>
            <el-option label="报废" value="报废"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">修改</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
import {getUser, LocalHost} from "@/utils/tools";

export default {
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      assetNumber: "",
      dialogVisible: false, // 添加 dialogVisible 属性并设置初始值为 false
      form: {},
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request
          .get("/maintain/outpage", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              assetNumber: this.assetNumber,
            },
          })
          .then((res) => {
            if (res.success) {
              this.tableData = res.data.records;
              this.total = res.data.total;
            }
          });
    },
    reset() {
      this.assetNumber = "";
      this.load();
    },
    exp() {
      window.open(LocalHost+ "/maintain/export");
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    handleEdit(index, row) {
      this.form.maintainRecord = row.maintainRecord;
      this.form.assetNumber = row.assetNumber;
      this.form.name = row.name;
      this.form.type = row.type;
      let user = getUser();
      this.form.maintenance = user.loginName;
      this.dialogVisible = true;
    },
    onSubmit() {
      // console.log(this.form)
      if (!this.form.maintenance || !this.form.result) {
        this.$message.error("请填写必填字段");
        return;
      }
      this.form.addtime = null;
      this.form.updateTime = null;
      this.request.post("/maintain/update", this.form).then((res) => {
        if (res.success) {
          res.addtime = this.addtime;
          this.$message.success("提交成功");
          this.dialogVisible = false; // 修正对话框可见属性名
          this.load();
        } else {
          this.$message.error("提交失败");
        }
      });
    },
  },
};
</script>